package com.gobo.jinghuimall.cart.controller;

import com.gobo.jinghuimall.cart.entiry.vo.Cart;
import com.gobo.jinghuimall.cart.entiry.vo.CartItem;
import com.gobo.jinghuimall.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 购物车的控制层
 */
@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/")
    public String toCartIndex(){
        return "cartList.html";
    }

    /**
     * 获取所有选中的商品
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/getCart/checked")
    @ResponseBody
    public List<CartItem> getCartByChecked() throws ExecutionException, InterruptedException {
        return cartService.getCartByChecked();
    }

    /**
     * 删除对应的商品
     * @param skuId
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.jinghuimall.com/cartList";
    }

    /**
     * 更改商品的选中状态
     * @param skuId
     * @param check
     * @param re
     * @return
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("check") Integer check, RedirectAttributes re) {
        Boolean isCheck = (check == 1) ? true : false;
        cartService.checkItem(skuId, isCheck);
        return "redirect:http://cart.jinghuimall.com/cartList";
    }

    /**
     * 更新数量
     * @param skuId
     * @param count
     * @return
     */
    @GetMapping("/countChange")
    public String countChange(@RequestParam("skuId") Long skuId, @RequestParam("count") Integer count) {
        cartService.countChange(skuId, count);
        return "redirect:http://cart.jinghuimall.com/cartList";
    }

    /**
     * 获取购物车数据
     * @param model
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/cartList")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        Cart cart = cartService.getCart();
        model.addAttribute("cart", cart);
        return "cartList";
    }

    @GetMapping("/cart.html")
    public String toCartListPage(Model model) throws ExecutionException, InterruptedException {
        Cart cart = cartService.getCart();
        model.addAttribute("cart", cart);
        return "cartList";
    }

    /**
     * 添加至购物车
     * @param skuId
     * @param count
     * @param redirectAttributes
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("count") Integer count, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId, count);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.jinghuimall.com/toSuccessPage.html";
    }

    /**
     * 跳转至成功界面
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/toSuccessPage.html")
    public String toSuccessPage(@RequestParam("skuId") Long skuId, Model model) {
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("cartItem", cartItem);
        return "success";
    }
}
